#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
//Left, Top, 0, Down, Right
bool pattern[][5] = {{true, true, false, false, false},
					{false, true, false, false, true},
					{true, false, false, true, false},
					{false, false, false, true, true},
					{false, true, false, true, false},
					{true, false, false, false, true},
					{true, true, false, false, true},
					{true, true, false, true, false},
					{true, false, false, true, true},
					{false, true, false, true, true},
					{true, true, false, true, true}};
int dir[] = {1, -1};
int MAP[52][52];
bool VIS[52][52];

bool tong(int s, int d, int dir) {
	int dir2 = -dir;
	if (pattern[s][dir+2] && pattern[d][dir2+2]) {
		return true;
	}
	return false;
}

int main() {
	while (true) {
		int M, N;
		scanf("%d%d", &M, &N);
		if (M==-1) break;
		getchar();
		for (int i=0; i<M; i++) {
			for (int j=0; j<N; j++) {
				char c;
				scanf("%c", &c);
				MAP[i][j] = c-'A';
			}
			getchar();
		}
		memset(VIS, 0, sizeof(VIS));
		int cnt=0;
		for (int i=0; i<M; i++) {
			for (int j=0; j<N; j++) {
				if (VIS[i][j]) continue;
				VIS[i][j] = true;
				cnt++;
				queue<int> q_i;
				queue<int> q_j;
				q_i.push(i);
				q_j.push(j);
				while (!q_i.empty()) {
					int ci = q_i.front(); q_i.pop();
					int cj = q_j.front(); q_j.pop();
					int c_type = MAP[ci][cj];
					if (ci+1 <= M-1) { //Down
						int n_type = MAP[ci+1][cj];
						if (!VIS[ci+1][cj]&&tong(c_type, n_type, 1)) {
							VIS[ci+1][cj] = true;
							q_i.push(ci+1);
							q_j.push(cj);
						}
					}
					if (ci-1 >= 0) { //Top
						int n_type = MAP[ci-1][cj];
						if (!VIS[ci-1][cj]&&tong(c_type, n_type, -1)) {
							VIS[ci-1][cj] = true;
							q_i.push(ci-1);
							q_j.push(cj);
						}
					}
					if (cj+1 <= N-1) { //Right
						int n_type = MAP[ci][cj+1];
						if (!VIS[ci][cj+1]&&tong(c_type, n_type, 2)) {
							VIS[ci][cj+1] = true;
							q_i.push(ci);
							q_j.push(cj+1);
						}
					}
					if (cj-1 >= 0) { //Left
						int n_type = MAP[ci][cj-1];
						if (!VIS[ci][cj-1]&&tong(c_type, n_type, -2)) {
							VIS[ci][cj-1] = true;
							q_i.push(ci);
							q_j.push(cj-1);
						}
					}
				}
			}
		}
		printf("%d\n", cnt);
	}
	return 0;
}
